#light

/// File: VSLabFSC\vslabfsc.fs
/// 
/// Author: Antonio Cisternino (cisterni@di.unipi.it)
///   
/// ------------------------------------------------------------
/// Copyright (c) Microsoft Corporation.  All rights reserved.
/// 
/// The use and distribution terms for this software are 
/// contained in the file named license.txt, which can be found 
/// in the root of this distribution.
/// By using this software in any fashion, you are agreeing to 
/// be bound by the terms of this license.
///
/// You must not remove this notice, or any other, from this
/// software.
/// ------------------------------------------------------------

// To compile please change the attribute name temporarily debugger in 
// HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\fsc.exe

open System
open System.Diagnostics
open Microsoft.Win32

let fetchFsBinDir () =
  use k = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Unipi\\VSLab")
  (k.GetValue("FSharpBin") :?> string)

printf "VSLab F# Compiler wrapper, to remove it please remove the Registry key:\n\nHKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Image File Execution Options\\fsc.exe\n\n"

let quote (s:string) =
  let v = s.Replace("\"", "\"\"\"")
  if v.IndexOf(" ") <> -1 then "\"" + v + "\"" else v

let mutable count = 0
let args = Array.filter (fun (s:string) -> let ret = count > 1 && not (s.ToLower().EndsWith(".vslab")) in count <- count + 1; ret ) (Environment.GetCommandLineArgs()) |>
             Array.map (fun (s:string) -> quote s)
//printf "ARGS: %s\n\n" (string.Join(" ", args))
let si = new ProcessStartInfo(fetchFsBinDir() + @"\fscvslab.exe", string.Join(" ", args))
si.UseShellExecute <- false

let p = new Process()
p.StartInfo <- si
p.Start()
p.WaitForExit()

Environment.ExitCode <- p.ExitCode